package com.czl.cloudservice.utils;

import lombok.extern.log4j.Log4j;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileOutputStream;

@Component
public class UploadFileUtil {
    public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception {
        File targetFile = new File(filePath);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        //如果文件存在就先删除了
        File ifFile = new File(filePath + File.separator + fileName);
        if (ifFile.exists()) {
            System.out.println("File is exists!");
            ifFile.delete();
        }
        //然后再写文件
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(filePath + File.separator + fileName);
            out.write(file);
        } catch (Exception e) {
            System.out.println("Error:"+e);
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

}
